﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics;

namespace wamr.Worker
{
    /// <summary>
    /// A backup task is made up of a copy and an export.  The job of this class is to 
    /// construct the Manager Messages for copy and export from the backup message.
    /// These messages are passed back to the worker role.
    /// </summary>
    public class BackupDatabaseTask:CompositeTask
    {
        //{"backup": {
        //    "overwritedatabase":"false",
        //    "overwriteblob":"false",
        //    "sqladminuser":"user name",
        //    "sqladminpw":"password",
        //    "source":
        //        {"servername":"xyz", "databasename":"abc"},
        //    "target":
        //        {"servername":"xyz", "databasename":"abc"},
        //    "storage":
        //        {"containername":"bacpac"}
        //    }
        //}

        private bool _overwriteTargetDatabase;
        private bool _overwriteBlob;
        private string _sqlAdminUserName;
        private string _sqlAdminPassword;
        private string _sourceServerName;
        private string _sourceDatabaseName;
        private string _targetServerName;
        private string _targetDatabaseName;
        private string _targetConnectionAlias;

        public BackupDatabaseTask(string ManagerMessage)
        {
            MessageIsValid = true;

            JSONClasses.JSONBackupRootObject r = JsonConvert.DeserializeObject<JSONClasses.JSONBackupRootObject>(ManagerMessage);

            _overwriteTargetDatabase = (r.backup.overwritedatabase == "true" ? true : false);
            _overwriteBlob = (r.backup.overwriteblob == "true" ? true : false);

            if (string.IsNullOrEmpty(r.backup.sqladminuser) || string.IsNullOrEmpty(r.backup.sqladminpw))
            {
                if (string.IsNullOrEmpty(r.backup.target.connectionalias))
                {
                    MessageIsValid = false;
                    Trace.TraceError("Either the sql admin creds or the target connection alias must be specified.");
                }
            }
            else
            {
                _sqlAdminUserName = r.backup.sqladminuser;
                _sqlAdminPassword = r.backup.sqladminpw;
            }

            if (string.IsNullOrEmpty(r.backup.source.servername) || string.IsNullOrEmpty(r.backup.source.databasename))
            {
                Trace.TraceError("Both the source server name and database name must be specified.");
                MessageIsValid = false;
            }
            else
            {
                _sourceServerName = r.backup.source.servername;
                _sourceDatabaseName = r.backup.source.databasename;
            }

            if (r.backup.target == null)
            // if the target section isn't included, copy the target server name from the source
            {
                _targetServerName = _sourceServerName;
            }
            else
            // otherwise, get the target server name and connection alias from the message
            {
                _targetServerName = r.backup.target.servername;
                _targetConnectionAlias = r.backup.target.connectionalias;
            }

            if (string.IsNullOrEmpty(r.backup.target.databasename))
                _targetDatabaseName = r.backup.source.databasename + "_copy";
            else
                _targetDatabaseName = r.backup.target.databasename;

        }

        public override List<string> TaskList()
        {
            List<string> l = new List<string>();

            JSONClasses.JSONSource source = new JSONClasses.JSONSource();
            source.databasename = _sourceDatabaseName;
            source.servername = _sourceServerName;

            JSONClasses.JSONTarget target = new JSONClasses.JSONTarget();
            target.servername = _targetServerName;
            target.databasename = _targetDatabaseName;
            target.connectionalias = _targetConnectionAlias;

            JSONClasses.JSONCopy copy = new JSONClasses.JSONCopy();
            copy.overwritedatabase = (_overwriteTargetDatabase ? "true" : "false");
            copy.source = source;
            copy.target = target;
            copy.sqladminpw = _sqlAdminPassword;
            copy.sqladminuser = _sqlAdminUserName;
            copy.dependency = Guid.NewGuid().ToString();

            JSONClasses.JSONCopyRootObject cro = new JSONClasses.JSONCopyRootObject();
            cro.copy = copy;

            string CopyMessage = JsonConvert.SerializeObject(cro);

            l.Add(CopyMessage);

            JSONClasses.JSONExport export = new JSONClasses.JSONExport();
            export.overwriteblob = (_overwriteBlob?"true":"false");
            export.source = source;
            export.target = target;
            export.sqladminpw = _sqlAdminPassword;
            export.sqladminuser = _sqlAdminUserName;
            export.dependson = copy.dependency;

            JSONClasses.JSONExportRootObject ero = new JSONClasses.JSONExportRootObject();
            ero.export = export;

            string ExportMessage = JsonConvert.SerializeObject(ero);

            l.Add(ExportMessage);

            return l;
        }
    }
}
